﻿using System;
using System.Collections.Generic;
using System.Linq;

using System.Web.Mvc;
using Prodma.DataAccess.Database;
using Prodma.DataAccess.Models;
using Prodma.DataAccess.Helper.Enumerators;
using Prodma.DataAccess.Models.ViewModels;
using System.Configuration;

namespace Prodma.DataAccess.DataService.ListService
{
    public static class CategoryList
    {

        private static DataContext context = new DataContext();

        #region Show CategoryName Menu Item Selected

        public static string GetCategoryName(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var name = (from entity in context.tb_Category
                            where entity.Id == id
                            select entity).FirstOrDefault();

                return name.Name;
            }
        }
        #endregion

        public static SelectList GetList()
        {
            DataContext context = new DataContext();
            using (context)
            {
                int stateId = Convert.ToInt32(State.Active);
                var list = (from category in context.tb_Category
                            where category.tb_State.Id == stateId
                            select category);
                SelectList selectList = new SelectList(list.ToDictionary(x => x.Id, z => z.Name), "Key", "Value");

                return selectList;
            }
        }
        public static SelectList GetList(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                int stateId = Convert.ToInt32(State.Active);
                var list = (from category in context.tb_Category
                            where category.tb_State.Id == stateId
                            select category);
                SelectList selectList = new SelectList(list.ToDictionary(x => x.Id, z => z.Name), "Key", "Value", id);

                return selectList;
            }
        }
        #region Menu Select Ajax

        public static IList<CategoryVm> GetListForMenu(int page)
        {
            int limitValue = Convert.ToInt32(ConfigurationManager.AppSettings["popupDataPagerLimit"]);
            int skipValue = page * limitValue;
            DataContext context = new DataContext();
            using (context)
            {
                var list = (from category in context.tb_Category
                            join order in context.tb_CategoryOrder
                                on category.Id equals order.Category
                            select new CategoryVm()
                            {
                                Id = category.Id,
                                Name = category.Name,
                                Title = category.Title,
                                Order = new CategoryOrderVm() { OrderNo = order.OrderNo },
                                Section = new SectionVm() { Id = category.tb_Section.Id, Name = category.tb_Section.Name },
                            }).OrderByDescending(x => x.Order.OrderNo).Skip(skipValue).Take(limitValue).ToList();

                return list;
            }
        }
        public static int GetTotalRowCountForMenu()
        {
            DataContext context = new DataContext();
            using (context)
            {
                int rowCount = (from category in context.tb_Category
                                select category).Count();
                return rowCount;
            }
        }
        public static IList<int> GetCountsForMenu()
        {
            DataContext context = new DataContext();
            using (context)
            {
                var countList = (from category in context.tb_Category
                                 group category by category.tb_Section.Id into section
                                 select section.Count()).ToList();

                return countList;
            }
        }
        public static IList<int> GetSectionListForMenu()
        {
            DataContext context = new DataContext();
            using (context)
            {
                var list = (from category in context.tb_Category
                            group category by category.tb_Section.Id into section
                            select section.Key).ToList();

                return list;
            }
        }
        #endregion

    }
}
